package com.nuanshui.heatedloan.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Validator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.nuanshui.heatedloan.extension.shiro.ShiroDbRealm;
import com.nuanshui.heatedloan.extension.shiro.ShiroDbRealm.ShiroUser;
import org.springframework.web.bind.annotation.ModelAttribute;

public class ApiController {
	protected Logger log = LoggerFactory.getLogger(getClass());
	private final static ThreadLocal<HttpServletRequest> request = new ThreadLocal<HttpServletRequest>();
	private final static ThreadLocal<HttpServletResponse> response = new ThreadLocal<HttpServletResponse>();
	protected final Integer DEFAULT_PAGE_SIZE = 20;
	public final String DEFAULTQUERYSTR = "{}";


	@SuppressWarnings("static-access")
	@ModelAttribute
	public void init(final HttpServletRequest request,
					 final HttpServletResponse response) {
		this.response.set(response);
		this.request.set(request);
	}

	@Autowired
	protected Validator validator;
	
	
	protected HttpServletRequest getRequest() {
		return request.get();
	}

	protected HttpServletResponse getResponse() {
		return response.get();
	}
	
	
	/**
	 * 默认返回success
	 * @return
	 */
	public String  success() {
		DefaultResponseObject result = new DefaultResponseObject();		    
	    return  JSON.toJSONString(result,SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNonStringKeyAsString);		
		
	}
	
	/**
	 * 默认失败返回
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String  fail() {
		ResponseObject result = new ResponseObject();	
		result.setStatus(GlobalErrorCode.FAIL);
	    return JSON.toJSONString(result);		
	}
	
	/**
	 * 返回失败，带上信息，前端就接data里面的数据！！
	 * @author Gonzo
	 * @date 2017年1月12日下午5:56:44
	 * @param obj
	 * @return
	 * @since 1.0.0
	 */
	@SuppressWarnings({ "rawtypes" })
	public String fail(String moreInfo){
		ResponseObject result = new ResponseObject();    
        result.setStatus(GlobalErrorCode.FAIL);
        result.setMoreInfo(moreInfo);
	    String  str = JSON.toJSONString(result);
	    return  str;	
	}
	
	/**
	 * 成功
	 * @param 
	 * @author Sven
	 * @date 2016年11月30日14:50:03
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String  success(Object obj) {
        ResponseObject result = new ResponseObject();    
        result.setData(obj);
	    String  str = JSON.toJSONString(result);
	    return  str;	
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String  success(Object obj, SerializerFeature e  ) {
        ResponseObject result = new ResponseObject();    
        result.setData(obj);
	    String  str = JSON.toJSONString(result,e);
	    return  str;	
	}
	
	
	/**
	 * 获取当前用户
	 * @author Gonzo
	 * @date 2017年1月12日下午3:33:24
	 * @return
	 * @since 1.0.0
	 */
	public ShiroUser getCurrentUser(){
		final Subject subject = SecurityUtils.getSubject();
		final ShiroUser shiroUser = (ShiroDbRealm.ShiroUser) subject.getPrincipal();
		return shiroUser;
	}
	
	
}
